<template>
	<scroll-view class="page" scroll-y="true" @scrolltolower="loadDataFn">
		
		<xzfx-banner v-if="bannerList.length > 0" :list="bannerList" />
		
		<view class="service">
			<view class="service-item" v-for="(item, index) in menus" :key="index" @click.stop="selectMenu(item)">
				<view class="service-item-name">{{ item.label.split("")[0] }}<text>{{ item.label.split("")[1] }}</text></view>
			</view>
		</view>
		
		<image v-if="advertisement" class="advertisement" :src="advertisement"></image>	
		
		<xzfx-panel icon="xzfx_icon_recommend" title="推荐车辆" bgColor="" @more="$go('/pages/car-sale/list')">
			<xzfx-car-list slot="content" :list="carList" />
		</xzfx-panel>
		
		<xzfx-divider>{{ dividerTitle }}</xzfx-divider>
	
	</scroll-view>
</template>

<script>
	export default {
		data() {
			return {
				menu: null,
				menus: [
					{
						label: "买车",
						path: "/pages/car-sale/explain-buy",
						needLogin: 0
					},
					{
						label: "卖车",
						path: "/pages/car-sale/explain-sale",
						needLogin: 0
					},
					{
						label: "估价",
						path: "/pages/car-evaluate/publish",
						needLogin: 1
					},
					{
						label: "置换",
						path: "/pages/car-replace/publish",
						needLogin: 1
					},
					{
						label: "求购",
						path: "/pages/want-buy/list",
						needLogin: 0
					},
					{
						label: "预约",
						path: "/pages/car-appt-sale/publish",
						needLogin: 1
					},
					{
						label: "拍卖",
						path: "/pages/car-auction/list",
						needLogin: 0
					}
				],
				advertisement: "",
				bannerList: [],
				waybillList: [],
				carList: [],
				total: 0,
				pageNo: 1,
				pageSize: 10,
				dividerTitle: "暂无更多"
			}
		},
		created() {
			this.getBannerFn();
			this.getDataFn();
		},
		onPullDownRefresh(){
			this.getBannerFn();
			this.carList = [];
			this.total = 0;
			this.pageNo = 1;
			this.pageSize = 10;
			this.getDataFn();
		},
		methods: {
			selectMenu(item){
				this.menu = item;
				if(item.needLogin === 1){
					this.$checkLogin(this.goSubPage)
				}else{
					this.goSubPage()
				}
			},
			goSubPage(){
				this.$go(this.menu.path)
			},
			loadDataFn(){
				if(this.total > this.carList.length){
					this.pageNo = this.pageNo + 1
					this.getDataFn()
				}else{
					this.dividerTitle = "暂无更多"
				}
			},
			getDataFn(){
				this.dividerTitle = "加载中......"
				this.$utils.system.loading("加载中，请稍后");
				this.$apis.carSale.getList({
					pageNo: this.pageNo,
					pageSize: this.pageSize
				}).then(res => {
					this.dividerTitle = "暂无更多"
					this.total = res.total
					this.carList = this.carList.concat(res.items)
				}).catch(err => {
					console.log(err)
					this.dividerTitle = "暂无更多";
				}).finally(() => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				});
			},
			getBannerFn(){
				this.$apis.config.banner().then(res => {
					this.bannerList = res.mainHeadBanner
					this.advertisement = res.mainCenterBanner && res.mainCenterBanner.length > 0 ? res.mainCenterBanner[0].imgUrl : ""
				}).catch(err => {
					this.bannerList = []
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.page{
		min-height: 100vh;
	}
	.advertisement{
		margin: $xzfx-spacing;
		width: calc(100% - 50rpx);
		height: calc((100vw - 50rpx) / 4 * 1);
		display: block;
		border-radius: 20rpx;
	}
	.service{
		margin: $xzfx-spacing;
		display: grid;
		grid-template-columns: repeat(4,1fr);
		gap: 15rpx;
		.service-item:nth-child(1){
			background-color: #FF5832;
			box-shadow: 0 8px 16px 0px #7e89ff29;
		}
		.service-item:nth-child(2){
			background-color: #ff822f;
			box-shadow: 0 8px 16px 0px #7e89ff29;
		}
		.service-item:nth-child(3){
			background-color: #FFAD4B;
			box-shadow: 0 8px 16px 0px #7e89ff29;
		}
		.service-item:nth-child(4){
			background-color: #7E89FF;
			box-shadow: 0 8px 16px 0px #7e89ff29;
		}
		.service-item:nth-child(5){
			background-color: #8dc63f;
			background-color: #7E89FF;
			box-shadow: 0 8px 16px 0px #7e89ff29;
		}
		.service-item:nth-child(6){
			background-color: #9c26b0;
			background-color: #FFAD4B;
			box-shadow: 0 8px 16px 0px #7e89ff29;
		}
		.service-item:nth-child(7){
			background-color: #a5673f;
			background-color: #ff822f;
			box-shadow: 0 8px 16px 0px #7e89ff29;
		}
		.service-item:nth-child(8){
			background-color: #1cbbb4;
			background-color: #FF5832;
			box-shadow: 0 8px 16px 0px #7e89ff29;
		}
		.service-item{
			height: 100rpx;
			background-image: url("https://bchr-truck.oss-cn-hangzhou.aliyuncs.com/truck-info/Gj48nEdXvzEN5a9c7fc1c3b3a42ce697728f1b775d75_1681802379657.png");
			background-repeat: no-repeat;
			background-size: 100% 120rpx;
			border-radius: 20rpx;
			color: #fff;
			display: flex;
			justify-content: center;
			align-items: center;
			.service-item-name{
				font-size: 48rpx;
				display: inline-flex;
				align-items: flex-end;
				text{
					font-size: 30rpx;
				}
			}
		}
	}
</style>
